﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Business.DA;
using Business.VOL;
using System.Data;

namespace Business.BLL
{
    class DMHangBL
    {
        private DataAccess dataAccess;
        public DMHangBL()
        {
            dataAccess = new DataAccess();
        }

        //Thêm
        public bool insertDMHang(DMHang obj)
        {
            SqlParameter[] param = new SqlParameter[7];
            param[0] = new SqlParameter("@TenHang", SqlDbType.NVarChar) { Value = obj.TenHang };
            param[1] = new SqlParameter("@HinhAnh", SqlDbType.NVarChar) { Value = obj.HinhAnh };
            param[2] = new SqlParameter("@DVT", SqlDbType.VarChar) { Value = obj.DVT };
            param[3] = new SqlParameter("@DonGia", SqlDbType.NVarChar) { Value = obj.DonGia };
            param[4] = new SqlParameter("@MaNhaCungCap", SqlDbType.Int) { Value = obj.MaNhaCungCap };
            param[5] = new SqlParameter("@MaLoaiHang", SqlDbType.Int) { Value = obj.MaLoaiHang };
            param[6] = new SqlParameter("@MoTa", SqlDbType.NVarChar) { Value = obj.MoTa };

            string strSql = "INSERT INTO tbl_DMHang(TenHang,HinhAnh,DVT,DonGia,MaNhaCungCap,MaLoaiHang,MoTa) VALUES (@TenHang,@HinhAnh,@DVT,@DonGia,@MaNhaCungCap,@MaLoaiHang,@MoTa)";
            dataAccess.Execute(strSql);

            return true;
        }

        //Xóa
        public bool deleteDMHang(DMHang obj)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHang", SqlDbType.Int) { Value = obj.MaHang };

            string strSql = "DELETE FROM tbl_DMHang WHERE MaHang = @MaHang";
            dataAccess.Execute(strSql);
            return true;
        }

        //Sửa
        public bool updateDMHang(DMHang obj)
        {
            SqlParameter[] param = new SqlParameter[8];
            param[0] = new SqlParameter("@TenHang", SqlDbType.NVarChar) { Value = obj.TenHang };
            param[1] = new SqlParameter("@HinhAnh", SqlDbType.NVarChar) { Value = obj.HinhAnh };
            param[2] = new SqlParameter("@DVT", SqlDbType.VarChar) { Value = obj.DVT };
            param[3] = new SqlParameter("@DonGia", SqlDbType.NVarChar) { Value = obj.DonGia };
            param[4] = new SqlParameter("@MaNhaCungCap", SqlDbType.Int) { Value = obj.MaNhaCungCap };
            param[5] = new SqlParameter("@MaLoaiHang", SqlDbType.Int) { Value = obj.MaLoaiHang };
            param[6] = new SqlParameter("@MoTa", SqlDbType.NVarChar) { Value = obj.MoTa };
            param[7] = new SqlParameter("@MaHang", SqlDbType.NVarChar) { Value = obj.MaHang };

            string strSql = "UPDATE tbl_DMHang SET " +
                            "TenHang = @TenHang, " +
                            "HinhAnh = @HinhAnh, " +
                            "DVT = @DVT, " +
                            "DonGia = @DonGia " +
                            "MaNhaCungCap = @MaNhaCungCap " +
                            "MaLoaiHang = @MaLoaiHang " +
                            "MoTa = @MoTa " +
                            "WHERE MaHang = @MaHang";
            dataAccess.Execute(strSql);
            return true;
        }

        //Tìm
        public DataTable searchIDDMHang(DMHang obj)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHang", SqlDbType.Int) { Value = obj.MaHang };

            string strSql = "SELECT * FROM tbl_DMHang WHERE MaHang = @MaHang";
            return dataAccess.GetData(strSql, param);
        }
    }
}
